package com.businessonthego.bizongo.controllers;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(eager=true)
@SessionScoped
public class LocaleManager implements Serializable {

	private static final long serialVersionUID = 1L;
	private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
	
	private static Map<String, String> countries;
	static {
		countries = new LinkedHashMap<String, String>();
		countries.put("English", "en"); // label, value
		countries.put("Swahili", "sw");
	}
	
	public Map<String, String> getCountriesInMap() {
		return countries;
	}

    public Locale getLocale() {
        return locale;
    }

    public String getLanguage() {
        return locale.getLanguage();
    }
    
    

    public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public void setLanguage(String language) {
       // locale = new Locale(language);
        setLocale(new Locale(language));
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }
    
 // value change event listener
 	public void countryLocaleCodeChanged(ValueChangeEvent e) {
         String newLocaleValue = e.getNewValue().toString();
         setLanguage(newLocaleValue);
        
 		}
 	

}
